package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.LoginService;
import com.itheima.service.impl.LoginServiceImpl;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


// 登入管理
@Slf4j
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public Result Login(@RequestBody Emp emp) {
        Emp emp1 = loginService.login(emp);
        /*登入成功，生成jwt令牌*/
        if (emp1!=null){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("id",emp1.getId());
            claims.put("username",emp1.getUsername());
            claims.put("name",emp1.getName());
            String jwt = JwtUtils.generateJwt(claims);
            return  Result.success(jwt);
        }
       return Result.error("用户密码错误");
    }
}
